package dataStructrue.tree.BinaryTree;

public class BinaryTree<T> {
    // 字符构造二叉树

    private TreeNode<T> root;
    // 二叉查找树的构造
    private int index=0;
    // 递归构造二叉树 先序构造
    public TreeNode<T> recursiveMethod(char[] data) {
        int i =0;
        if (data[i]=='(') {

        }

        return null;
    }

    // 非递归构造二叉树
    public void createTree(char[] data) {
        int top =-1;
        int k=0; //1处理子树二处理右子树
        TreeNode[] stack = new TreeNode[20];
        int i = 0;
        TreeNode temp = null;
        char  ch;
        while (i<data.length) {
            ch = data[i];
            switch (ch) {
                case '(':{
                    top++;
                    k=1;
                    stack[top] = temp;
                    break;
                }
                case ')':{
                    top--;
                    break;
                }
                case ',':{
                    k=2;
                    break;
                }
                default:{
                    temp = new TreeNode(ch,null,null);
                    temp.data = ch;
                    if(root==null) {
                        root=temp;
                    }
                    switch (k) {
                        case 1:{
                            stack[top].left = temp;
                            break;
                        }
                        case 2: {
                            stack[top].right = temp;
                            break;
                        }
                    }
                }
            }
            i++;
        }
    }

    // 中序遍历
    public void middle_scan(TreeNode treeNode) {
        if(treeNode!=null) {
            middle_scan(treeNode.left);
            System.out.print(treeNode.data+" ");
            middle_scan(treeNode.right
            );
        }
    }
    public static void main(String[] args) {
        BinaryTree<Integer> binaryTree = new BinaryTree<>();
        binaryTree.createTree("A(B(E,F(,G)),C(,D))".toCharArray());
        System.out.println(binaryTree.root.data);
        System.out.println(binaryTree.root.left.data);
        System.out.println(binaryTree.root.right.data);
        binaryTree.middle_scan(binaryTree.root);
    }
}
